package com.coffee.sku.service.sku.impl;

import com.coffee.admin.service.common.bean.GtRegion;
import com.coffee.admin.service.common.dao.GtRegionDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.service.sku.bean.EnjoyBeforePaySku;
import com.coffee.order.service.sku.dao.EnjoyBeforePaySkuDao;
import com.coffee.sku.api.sku.query.RecommendSkuQuery;
import com.coffee.sku.api.sku.query.RegionalSkuSalesInformationQuery;
import com.coffee.sku.api.sku.query.SkuSalesInformationQuery;
import com.coffee.sku.api.sku.req.MerchantSkuListReq;
import com.coffee.sku.api.sku.req.SkuListReq;
import com.coffee.sku.api.sku.result.*;
import com.coffee.sku.api.sku.service.RecommendSkuService;
import com.coffee.sku.service.sku.bean.RecommendSku;
import com.coffee.sku.service.sku.bean.RegionalSkuSalesInformation;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.bean.SkuSalesInformation;
import com.coffee.sku.service.sku.dao.RecommendSkuDao;
import com.coffee.sku.service.sku.dao.RegionalSkuSalesInformationDao;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import com.coffee.sku.service.sku.dao.SkuSalesInformationDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class RecommendSkuServiceImpl implements RecommendSkuService {
    @Resource
    private RecommendSkuDao recommendSkuDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private SkuSalesInformationDao skuSalesInformationDao;
    @Resource
    private GtRegionDao gtRegionDao;
    @Resource
    private EnjoyBeforePaySkuDao enjoyBeforePaySkuDao;
    @Resource
    private RegionalSkuSalesInformationDao regionalSkuSalesInformationDao;

    @Override
    public Page<RecommendSkuResult> getPager(RecommendSkuQuery param, PagerInfo pr) {
        Page<RecommendSkuResult> pager = new Page<RecommendSkuResult>(pr.getStart(), pr.getPageSize());
        Integer count = recommendSkuDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<RecommendSku> list = recommendSkuDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, RecommendSkuResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(RecommendSkuQuery param) {
        Integer count = recommendSkuDao.count(param);
        return count;
    }

    @Override
    public RecommendSkuResult getDetail(Integer id) {
        RecommendSku item = recommendSkuDao.findById(id);
        if (item == null) {
            return null;
        }
        RecommendSkuResult result = new RecommendSkuResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<RecommendSkuResult> getList(RecommendSkuQuery param, PagerInfo pr) {
        List<RecommendSku> list = recommendSkuDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, RecommendSkuResult.class);
    }

    @Override
    public List<SkuListResult> getList(SkuListReq param) {
        List<SkuListResult> list = recommendSkuDao.list(param);
        if (list != null && list.size() > 0) {
            for (SkuListResult skuListRep : list) {
                if (StringUtils.isNotEmpty(skuListRep.getPidPath())) {
                    String[] split = skuListRep.getPidPath().split(",");
                    SkuCategory skuCategory = skuCategoryDao.findById(Integer.parseInt(split[0]));
                    if (skuCategory != null) {
                        skuListRep.setCategoryName(skuCategory.getName());
                    }
                }
                SkuSalesInformationQuery skuSalesInformationQuery = new SkuSalesInformationQuery();
                skuSalesInformationQuery.setSkuId(skuListRep.getSkuId());
                List<SkuSalesInformation> skuSalesInformations = skuSalesInformationDao.list(skuSalesInformationQuery);
                skuListRep.setSkuSalesInformationResults(DTOUtils.convertList(skuSalesInformations, SkuSalesInformationResult.class));
            }
        }
        return list;
    }

    @Override
    public List<RecommendSkuResult> getList() {
        RecommendSkuQuery param = new RecommendSkuQuery();
        List<RecommendSku> list = recommendSkuDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, RecommendSkuResult.class);
    }

    @Override
    public List<MerchantSkuListResult> getMerchantSkuListResult(MerchantSkuListReq req) {
        List<MerchantSkuListResult> merchantSkuListResults = recommendSkuDao.merchantSkuList(req);
        Integer skuIdsCount = regionalSkuSalesInformationDao.getSkuIdsCount(req);
        if (merchantSkuListResults != null && merchantSkuListResults.size() > 0) {
            Iterator<MerchantSkuListResult> iterator = merchantSkuListResults.iterator();
            while (iterator.hasNext()) {
                MerchantSkuListResult merchantSkuListRep = iterator.next();
                if (merchantSkuListRep.getSpellOrderCityId() != null) {
                    GtRegion gtRegion = gtRegionDao.findById(merchantSkuListRep.getSpellOrderCityId());
                    merchantSkuListRep.setSpellOrderCity(gtRegion.getName());
                }
                SkuSalesInformationQuery param = new SkuSalesInformationQuery();
                param.setSkuId(merchantSkuListRep.getId());
                SkuSalesInformation skuSalesInformationDaoFirst = skuSalesInformationDao.findFirst(param);
                merchantSkuListRep.setPrice(skuSalesInformationDaoFirst.getPrice());
                if (req.getCityId() != null && skuIdsCount > 0) {
                    RegionalSkuSalesInformationQuery regionalSkuSalesInformationQuery = new RegionalSkuSalesInformationQuery();
                    regionalSkuSalesInformationQuery.setGtRegionId(req.getCityId());
                    regionalSkuSalesInformationQuery.setSkuId(skuSalesInformationDaoFirst.getSkuId());
                    RegionalSkuSalesInformation regionalSkuSalesInformationDaoFirst = regionalSkuSalesInformationDao.findFirst(regionalSkuSalesInformationQuery);
                    if (regionalSkuSalesInformationDaoFirst != null) {
                        merchantSkuListRep.setPrice(regionalSkuSalesInformationDaoFirst.getRegionalPrice());
                    } else {
                        iterator.remove();
                    }
                }
                if ("Y".equals(merchantSkuListRep.getIsSpellOrder())) {
                    merchantSkuListRep.setPrice(skuSalesInformationDaoFirst.getSpellOrderPrice());
                }
                EnjoyBeforePaySku enjoyBeforePaySku = enjoyBeforePaySkuDao.findBySkuId(merchantSkuListRep.getId());
                if (enjoyBeforePaySku != null) {
                    merchantSkuListRep.setIsEnjoy("Y");
                }
            }
            for (MerchantSkuListResult merchantSkuListRep : merchantSkuListResults) {

            }
        }
        return merchantSkuListResults;
    }


    @Override
    public void delete(Integer id) {
        recommendSkuDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(RecommendSkuResult result) throws BusinessException {
        RecommendSku item = new RecommendSku();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            recommendSkuDao.insert(item);
        } else {
            RecommendSku tmp = recommendSkuDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            recommendSkuDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, RecommendSkuResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, RecommendSkuResult> map = new HashMap<Integer, RecommendSkuResult>();
        List<RecommendSku> list = recommendSkuDao.findByIds(StringUtil.setToList(idSet));
        List<RecommendSkuResult> resultList = DTOUtils.convertList(list, RecommendSkuResult.class);
        for (RecommendSkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, RecommendSkuResult> getItemMap() {
        Map<Integer, RecommendSkuResult> map = new HashMap<Integer, RecommendSkuResult>();
        RecommendSkuQuery param = new RecommendSkuQuery();
        List<RecommendSku> list = recommendSkuDao.page(param, 0, Integer.MAX_VALUE);
        List<RecommendSkuResult> resultList = DTOUtils.convertList(list, RecommendSkuResult.class);
        for (RecommendSkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


    @Override
    public void deleteBySkuId(Integer id) {
        recommendSkuDao.deleteBySkuId(id);
    }


    @Override
    public void addBySkuIds(List<Integer> skuIds, Integer cityId) throws BusinessException {
        if (skuIds.size() < 4 || skuIds.size() > 8) {
            throw BusinessException.build(ExceptionEnum.ERROR_1352);
        }
        List<Integer> existenceSkuIds = recommendSkuDao.findSkuIds(cityId);
        Collection deleteSubtract = CollectionUtils.subtract(existenceSkuIds, skuIds);
        List<Integer> deleteAddList = new ArrayList<>(deleteSubtract);
        recommendSkuDao.deleteBySkuIds(deleteAddList, cityId);
        Collection subtract = CollectionUtils.subtract(skuIds, existenceSkuIds);
        List<Integer> addList = new ArrayList<>(subtract);
        if (addList.size() > 0) {
            List<RecommendSku> list = new ArrayList<>();
            for (Integer integer : addList) {
                RecommendSku param = new RecommendSku();
                param.setSkuId(integer);
                param.setCityId(cityId);
                list.add(param);
            }
            recommendSkuDao.insertBatch(list);
        }

    }

    @Override
    public void changSort(List<Integer> skuIds, Integer cityId) throws BusinessException {
        for (int i = 0; i < skuIds.size(); i++) {
            RecommendSku param = new RecommendSku();
            param.setSort(i);
            param.setCityId(cityId);
            param.setSkuId(skuIds.get(i));
            recommendSkuDao.updateById(param);
        }

    }


}
